package com.bfl.sa.common.utils;

import com.bfl.sa.common.entity.BaseEntity;
import com.bfl.sa.common.plugin.entity.Treeable;
import com.bfl.sa.common.plugin.web.controller.entity.ZTree;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.List;

public class ZTreeUtils<K extends Serializable, M extends BaseEntity<K> & Treeable<K>> {

    public ZTree<K> convertToZtree(M m, boolean open, boolean onlyCheckLeaf) {
        ZTree<K> zTree = new ZTree<>();
        zTree.setId(m.getId());
        zTree.setpId(m.getParentId());
        zTree.setName(m.getName());
        zTree.setIconSkin(m.getIcon());
        zTree.setOpen(open);
        zTree.setRoot(m.isRoot());
        zTree.setIsParent(m.isHasChildren());

        if (onlyCheckLeaf && zTree.isIsParent()) {
            zTree.setNocheck(true);
        } else {
            zTree.setNocheck(false);
        }

        return zTree;
    }

    public List<ZTree<K>> convertToZtreeList(List<M> models, boolean async, boolean onlySelectLeaf) {
        List<ZTree<K>> zTrees = Lists.newArrayList();

        if (models == null || models.isEmpty()) {
            return zTrees;
        }

        for (M m : models) {
            ZTree<K> zTree = convertToZtree(m, !async, onlySelectLeaf);
            zTrees.add(zTree);
        }
        return zTrees;
    }

    public List<ZTree<K>> convertToZtreeList(String contextPath,  List<M> models, boolean async, boolean onlySelectLeaf) {
        return convertToZtreeList(models, async, onlySelectLeaf);
    }

}
